package com.heer.gymnasium.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.heer.gymnasium.AppHelper;
import com.heer.gymnasium.R;
import com.heer.gymnasium.util.ActivityStartUtils;
import com.heer.gymnasium.util.TitleBuilder;

import butterknife.ButterKnife;
import cn.sharesdk.framework.ShareSDK;

/**
 * Created by heer on 16/6/1.
 */
public class BaseActivity extends AppCompatActivity {
    protected String TAG;

    protected TitleBuilder titleBuilder;
    protected Context mContext;
    protected AppHelper mAppHelper;
    protected ActivityStartUtils activityStartUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        initView();
        loadData();
    }

    protected void init() {
        TAG = getComponentName().getClassName();

        Log.e(TAG, "init: ");

        mContext = this;
        mAppHelper = AppHelper.context();
        activityStartUtils = new ActivityStartUtils(this);
        ShareSDK.initSDK(mContext);
    }

    protected void initView() {
        Log.e(TAG, "initView: ");
        ButterKnife.bind(this);
        setActionBarLayout(R.layout.layout_default_top, this);
    }

    protected void loadData() {
        loadData(false);
    }

    protected void loadData(boolean loadmore) {
        Log.e(TAG, "loadData: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e(TAG, "onStop: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: ");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e(TAG, "onStart: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e(TAG, "onPause: ");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.e(TAG, "onRestoreInstanceState: ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(TAG, "onRestart: ");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.e(TAG, "onSaveInstanceState: ");
    }

    /**
     * 设置ActionBar的布局
     *
     * @param layoutId 布局Id
     */
    @SuppressLint("NewApi")
    public void setActionBarLayout(int layoutId, Context mContext) {
        ActionBar actionBar = getSupportActionBar();
        if (null != actionBar) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            View v = inflater.inflate(layoutId, new LinearLayout(mContext), false);
            ActionBar.LayoutParams layout = new ActionBar.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            actionBar.setCustomView(v, layout);
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            titleBuilder = new TitleBuilder(v);
            titleBuilder.setLeftImage(R.mipmap.ic_back).setLeftOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();

                }
            });
        }
    }

    protected void toLogin(){
        Intent intent = new Intent(mContext, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
